#include "qs60Telephony_p.h"
#include <EIKENV.H>
#include <e32base.h>
#include <QDebug>

QS60TelephonyPrivate::QS60TelephonyPrivate(QS60Telephony* telephony) :
    q(telephony)
{
    iTelephony        = CTelephony::NewL();
    iTelephonyMonitor = new CTelephonyMonitor(*this, iTelephony);
    iCallDialer       = new CCallDialer(*this, iTelephony);
    iCallAnswerer     = new CCallAnswerer(*this, iTelephony);
    iCallTerminator   = new CCallTerminator(*this, iTelephony);    
#ifdef RECENT_CALLS
    iRecentCalls      = CRecentCalls::NewL();
#endif

}

QS60TelephonyPrivate::~QS60TelephonyPrivate()
{
    delete iCallDialer;
    delete iCallAnswerer;
    delete iCallTerminator;
    delete iTelephonyMonitor;
    delete iTelephony;    
}

bool QS60TelephonyPrivate::startMonitoringLine()
{
    TRAP(iError,         
        if (!iTelephonyMonitor) {            
            iTelephonyMonitor = new CTelephonyMonitor(*this, iTelephony);
        }
        iTelephonyMonitor->StartListening();
    )
    return (iError == KErrNone);
}

void QS60TelephonyPrivate::stopMonitoringLine()
{
    delete iTelephonyMonitor;
    iTelephonyMonitor == NULL;
}

void QS60TelephonyPrivate::call(const QString& phoneNumber)
{
    TPtrC16 textPtr(reinterpret_cast<const TUint16*>(phoneNumber.utf16()));
    TRAP(iError,
        if (!iCallDialer) {
            //iCallDialer = CCallDialer::NewL(*this);
             iCallDialer = new CCallDialer(*this, iTelephony);
        }
        iCallDialer->Call(textPtr);
    )
}

void QS60TelephonyPrivate::answerIncomingCall(){
    qDebug() << "[step #4] Answering incoming call";
    TRAP(iError,
        if (!iCallAnswerer) {
            iCallAnswerer = new CCallAnswerer(*this, iTelephony);
        }
        iCallAnswerer->Answer();
    )
}

void QS60TelephonyPrivate::terminateCall() {  
    TRAP(iError,
       if (!iCallTerminator) {
         iCallTerminator = new CCallTerminator(*this, iTelephony);
       }
       iCallTerminator->Terminate();
   )
}

void QS60TelephonyPrivate::rejectCall() {
    TRAP(iError,
        if (!iCallTerminator) {
            iCallTerminator = new CCallTerminator(*this, iTelephony);
        }
        iCallTerminator->Reject();
    )
}

void QS60TelephonyPrivate::TelephonyStatusChangedL(
    CTelephony::TCallStatus aStatus, const TDesC& number){
    QString callerNumber = QString::fromUtf16(number.Ptr(), number.Length());
    q->cStatus = static_cast<QS60Telephony::LineStatus>(aStatus);
    emit q->lineStatusChanged(static_cast<QS60Telephony::LineStatus>(aStatus), callerNumber);
}

void QS60TelephonyPrivate::CallDialedL(TInt aError)
{
    iError = aError;
    if (iError != KErrNone) {
        emit q->error(error());
    }
}

void QS60TelephonyPrivate::CallAnsweredL(TInt aError)
{
    iError = aError;
    if (iError == KErrNone) {
        emit q->answered();
    } else {
        emit q->error(error());
    }
}


void QS60TelephonyPrivate::CallTerminatedL(TInt aError)
{
    iError = aError;
    if (iError == KErrNone) {
        emit q->terminated();
    } else {
        emit q->error(error());
    }
}

void QS60TelephonyPrivate::CallRejectedL(TInt aError)
{
    iError = aError;
    if (iError == KErrNone) {
        emit q->terminated();
    } else {
        emit q->error(error());
    }
}


void QS60TelephonyPrivate::ErrorOccuredL(TInt aError)
{
    iError = aError;
    emit q->error(error());   
}

QS60Telephony::Error QS60TelephonyPrivate::error()
{
    qDebug() << "Error#"<<QString::number(iError);
    switch (iError) {
    case KErrNone:
        return QS60Telephony::NoError;
    case KErrNoMemory:
        return QS60Telephony::OutOfMemoryError;
    case KErrAccessDenied:
        return QS60Telephony::AccessDeniedError;
    case KErrTimedOut:
        return QS60Telephony::TimedOutError;
    default:
        return QS60Telephony::UnknownError;
    }    
}
#ifdef RECENT_CALLS
void QS60TelephonyPrivate::recentCalls(){
    TRAP(iError,
        if (!iRecentCalls) {
            iRecentCalls = CRecentCalls::NewL();
        }
        iRecentCalls->ReadRecentEventsL();
    )
}
#endif

void QS60TelephonyPrivate::toBackground() {
    CEikonEnv::Static()->RootWin().SetOrdinalPosition(-1, ECoeWinPriorityNormal);
    qDebug() << "toBackground";
}

void QS60TelephonyPrivate::showNormal() {
    CEikonEnv::Static()->RootWin().SetOrdinalPosition(0, ECoeWinPriorityNormal);
    qDebug() << "showNormal";
}

void QS60TelephonyPrivate::showOnTop() {    
    CEikonEnv::Static()->RootWin().SetOrdinalPosition(0, ECoeWinPriorityAlwaysAtFront);
    qDebug() << "showOnTop";
}

QString QS60TelephonyPrivate::IMEI(){
    TBuf<25> iIMEI;
    CIMEIApp::GetIMEI(iIMEI);    
    QString qimei((QChar*)iIMEI.Ptr(),iIMEI.Length());
    return qimei;
}

// End of file
